package com.cyan.app.workflow.activity.SFC;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.cyan.app.workflow.R;
import com.cyan.app.workflow.base.activity.BaseActivity;
import com.cyan.app.workflow.customview.PullDownView;
import com.cyan.app.workflow.util.FileUtils;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by DXW on 2016/2/19.
 * 工单入库 - 仓库类型
 */
public class GdlkCkActivity extends BaseActivity implements PullDownView.OnPullDownListener, AdapterView.OnItemClickListener {
    private List<WarehBean> mWarehBeans;
    private GdlkCkAdapter adapter;
    private ListView prossinfo_lv;
    private PullDownView mPullDownView;
    private Boolean isPressMore = false;
    private int pageCount = 15;
    /** 请求第几页数据 */
    private int intPageNo = 1;// 0
    /** 是否有下一页 */
    private boolean nextPage = false;
    private List<WarehBean> mWarehDatas = new ArrayList<WarehBean>();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sfc_gdlk_ck);
        initData();
        initView();
        startProssThread();
    }
    @Override
    protected void onResume() {
        super.onResume();
        adapter.notifyDataSetChanged();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }

    private void initView() {
        titleBar();
        /*
		 * 1.使用PullDownView 2.设置OnPullDownListener 3.从mPullDownView里面获取ListView
		 */
        mPullDownView = (PullDownView) findViewById(R.id.sfc_gdlk_ck_lv);

        mPullDownView.setOnPullDownListener(this);

        prossinfo_lv = mPullDownView.getListView();

        //prossinfo_lv.setOnItemClickListener(this);
        prossinfo_lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = GdlkCkActivity.this.getIntent();
                //Bundle bundle=new Bundle();
                //bundle.putString("prosscode",mProssDatas.get(position).getProsscode());
                intent.putExtra("wareh",mWarehDatas.get(position).getWareh());
                GdlkCkActivity.this.setResult(RESULT_OK, intent);
                finish();
            }
        });
        // 隐藏并且禁用头部刷新
        mPullDownView.setHideHeader();
    }

    private void initData() {
        String json = null;
        try {
            json = FileUtils.getFileContent(this.getAssets().open("wareh.json"));
            mWarehBeans = new Gson().fromJson(json, new TypeToken<List<WarehBean>>() {
            }.getType());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private void getData(int PageNo){
        if(mWarehBeans==null) return;
        int count = (intPageNo*pageCount)> mWarehBeans.size()?mWarehBeans.size():intPageNo*pageCount;
        for(int i = (intPageNo-1)* pageCount;i< count;i++){
            mWarehDatas.add(mWarehBeans.get(i));
        }
    }
    private void  showList(){
        // 获取更多数据之外的请求，如首次请求数据，
        if (!isPressMore) {
            getData(intPageNo);
            adapter = new GdlkCkAdapter(GdlkCkActivity.this);
            prossinfo_lv.setAdapter(adapter);

        }else {
            // 获取更多数据
            getData(intPageNo);
            adapter.notifyDataSetChanged();
        }
        // 判断是否有下一页
        if (mWarehBeans.size() > mWarehDatas.size()) {
            // 显示并启用自动获取更多
            mPullDownView.setShowFooter();
            // 设置可以自动获取更多 滑到最后一个自动获取 改成false将禁用自动获取更多
            mPullDownView.enableAutoFetchMore(true, 1);
        } else {
            // 隐藏 并禁用尾部
            mPullDownView.setHideFooter();
            // 设置可以自动获取更多 滑到最后一个自动获取 改成false将禁用自动获取更多
            mPullDownView.enableAutoFetchMore(false, 1);
        }
        // 告诉它获取更多完毕 这个事线程安全的 可看源代码
        mPullDownView.notifyDidMore();

        isPressMore = false;

        //prossinfo_lv.setOnItemClickListener(this.onItemClick(););
    }
    private void titleBar() {
        ((TextView) findViewById(R.id.top_title_info)).setText("仓库列表");
        Button back = (Button) findViewById(R.id.top_title_back);
        back.getBackground().setAlpha(255);// 透明度0~255透明度值 ，值越小越透明
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //返回按钮
                finish();
            }
        });
    }
    /**
     * listview单项点击事件
     */
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent intent = GdlkCkActivity.this.getIntent();
        //Bundle bundle=new Bundle();
        //bundle.putString("prosscode",mProssDatas.get(position).getProsscode());
        intent.putExtra("prosscode",mWarehDatas.get(position).getWareh());
        GdlkCkActivity.this.setResult(RESULT_OK, intent);
        finish();
    }

    /**
     * listview刷新
     */
    public void onRefresh() {

    }

    /**
     * listview更多
     */
    public void onMore() {
        isPressMore = true;
        // 发送更多请求
        intPageNo++;
        startProssThread();
    }
    // 启动线程
    private void startProssThread() {
        // Log.d("vans", issueid+"------"+intPageNo);
        showList();
    }
    public class GdlkCkAdapter extends BaseAdapter {
        private LayoutInflater inflater = null;

        public GdlkCkAdapter(Context context) {
            inflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return mWarehDatas.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            ViewHolder holder = null;
            holder = new ViewHolder();
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            layoutParams.gravity = Gravity.CENTER_VERTICAL;
            convertView = inflater.inflate(R.layout.sfc_gdlk_ck_list, null);
            holder.sfc_ck_list_name = (TextView) convertView.findViewById(R.id.sfc_ck_list_name);
            holder.sfc_ck_list_code = (TextView) convertView.findViewById(R.id.sfc_ck_list_code);
            holder.sfc_ck_list_selected = (CheckBox) convertView.findViewById(R.id.sfc_ck_list_selected);
            holder.sfc_ck_list_name.setText(mWarehDatas.get(position).getWhdsc());
            holder.sfc_ck_list_code.setText(mWarehDatas.get(position).getWareh());
            holder.sfc_ck_list_selected.setChecked(false);

            holder.sfc_ck_list_selected.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if(isChecked){
                        Intent intent = GdlkCkActivity.this.getIntent();
                        //Bundle bundle=new Bundle();
                        //bundle.putString("prosscode",mProssDatas.get(position).getProsscode());
                        intent.putExtra("wareh",mWarehDatas.get(position).getWareh());
                        GdlkCkActivity.this.setResult(RESULT_OK, intent);
                        finish();
                    }
                }
            });

            convertView.setTag(holder);

            return convertView;
        }

        class ViewHolder {
            TextView sfc_ck_list_code;
            TextView sfc_ck_list_name;
            CheckBox sfc_ck_list_selected;
        }
    }

      class WarehBean {

        private String Wareh;
        private String Whdsc;

        public String getWareh() {
            return Wareh;
        }

        public void setWareh(String prosscode) {
            this.Wareh = prosscode;
        }

        public String getWhdsc() {
            return Whdsc;
        }
        public void setWhdsc(String prossname) {
            this.Whdsc = prossname;
        }
    }
}
